import _ from 'lodash';
import { message, Select } from 'antd';
import { NAME_SPACE, SearchParams, DetailsSearchParams } from '../components/CollectionAmountMonitor/config';
import { getCountPageList, getAllUserAndAllCar, getInfoPageList, showImage } from '../services/collectionAmountMonitor';
import moment from 'moment';

export default {
  namespace: NAME_SPACE,
  state: {
    loading: false,
    pageNo: 1,
    pageSize: 20,
    total: 0,
    searchParams: {...SearchParams},
    oldSearchParams: {...SearchParams},
    dataList:[],
     // 详情可见
     detailsModelVisable: false,
     details: [],
     detailsLoading: false,
     detailsPageNo: 1,
     detailsPageSize: 20,
     detailsTotal: 0,
     detailsSearchParams: {...DetailsSearchParams},
     oldDetailsSearchParams: {...DetailsSearchParams},
    //  是否刷新
    refresh: true,
    // 图片可见
    picVisable: false,
    // 可选
    choice: false,
    // 人员列表
    userList: [],
    // 车辆列表
    carList: [],
    // 详情id
    modifyId: '',
    // 图片列表
    imgList:[],
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({pathname, query}) => {
        if(pathname == '/collectionAmountMonitor'){
          dispatch({
            type:'init',
            payload:{}
          });
        }
      });
    },
  },
  effects: {
    *init({payload},{select, put, call}){
      yield put({
        type: 'getAllUserAndAllCar',
        payload:{} 
      });

      yield put({
        type: 'getList',
        payload: {type: 'search'}
      })
    },
    *getList({payload},{select, put, call}){
      let {type: searchType, delNum} = payload;
      let {searchParams, oldSearchParams, total, pageSize, pageNo} = yield select(state => state[NAME_SPACE]);
      let newPageNo = 1, newPageSize = 20;
      switch (searchType) {
        case "search":
            oldSearchParams = Object.assign({}, searchParams);
            break;
        case "page":
            newPageNo = pageNo;
            newPageSize = pageSize;
        break;
            case "del":
            if (total===1 || (total-(pageNo)*pageSize - delNum)>0) {
                newPageNo = pageNo;
            }else{
                newPageNo = pageNo-1;
            }
            break;
        default :
            break;
      }
      yield put({
        type: 'updateState',
        payload: {
          loading: true
        }
      })
      const result = yield call(getCountPageList, {
        ...oldSearchParams, 
        rows: newPageSize, 
        page: newPageNo,
      })
      // console.log(result)
      if(result.rc == 0){
        yield put({
          type: 'updateState',
          payload: {
            dataList: result.ret.items,
            total: result.ret.count,
            pageNo: newPageNo,
            pageSize: newPageSize,
            loading: false,
          }
        })
      }else{
        yield put({
          type: 'updateState',
          payload: {
            dataList: [],
            total: 0,
            pageNo: 1,
            pageSize: 20,
            loading: false,
          }
        })
      }

    },
    *getDetailsList({payload},{select, put, call}){
      let {id, type: searchType, delNum} = payload;
      let {detailsSearchParams, oldDetailsSearchParams, detailsTotal, detailsPageSize, detailsPageNo, modifyId} = yield select(state => state[NAME_SPACE]);
      let newPageNo = 1, newPageSize = 20;
      switch (searchType) {
        case "search":
          oldDetailsSearchParams = Object.assign({}, detailsSearchParams);
            break;
        case "page":
            newPageNo = detailsPageNo;
            newPageSize = detailsPageSize;
        break;
            case "del":
            if (detailsTotal===1 || (detailsTotal-(detailsPageNo)*detailsPageSize - delNum)>0) {
                newPageNo = detailsPageNo;
            }else{
                newPageNo = detailsPageSize-1;
            }
            break;
        default :
            break;
      }
      const result = yield call(getInfoPageList, {...oldDetailsSearchParams, rows: newPageSize, page: newPageNo,})
      if(result.rc == 0){
        yield put({
          type: 'updateState',
          payload: {
            details: result.ret.items,
            detailsTotal: result.ret.count,
            detailsPageNo: newPageNo,
            detailsPageSize: newPageSize,
            detailsLoading: false,
          }
        })
      }else{
        yield put({
          type: 'updateState',
          payload: {
            details: [],
            detailsTotal: 0,
            detailsPageNo: 1,
            detailsPageSize: 20,
            detailsLoading: false,
          }
        })
      }
    },
    *getAllUserAndAllCar({payload}, {select, call, put}){
      const result = yield call(getAllUserAndAllCar, {});
      // console.log(result)
      if(result.rc == 0){
        yield put({
          type: 'updateState',
          payload: {
            userList: result.ret.userList,
            carList: result.ret.carList
          }
        })
      }
    },
    *showImage({payload}, {select, call, put}){
      const {item} = payload
      console.log(item)
      const result = yield call(showImage, {filepath: item})

    }
  },
  reducers:{
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
    updateSubState(state, { payload }) {
        let { data, sub } = payload;
        return {
            ...state,
            [sub]: {
                ...state[sub],
                ...data,
            }
        }
    },
  },
}